/**
 * ModGames (C) 2007 Pedro Miller Rabinovitch.
 * 
 * Permission is hereby granted to copy and use this material for any free or educational purpose.
 * Please do not use this for commercial purposes before letting me know (but it'll be fine).
 * 
 * Contact me via gmail, account pedromr
 * http://exodia.org
 * 
 * $Id: SnakePiece.java 20 2007-11-28 09:59:46Z pedromr $
 */
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

public class SnakePiece extends Sprite {
	public int col, row;

	public SnakePiece ahead;

	public int orientation;

	static final int O_DOWN = 1;

	static final int O_LEFT = 2;

	static final int O_UP = 3;

	static final int O_RIGHT = 4;

	public SnakePiece(Image image, int frameWidth, int frameHeight,
			SnakePiece ahead) {
		super(image, frameWidth, frameHeight);
		defineReferencePixel(frameWidth/2, frameHeight/2);
		this.ahead = ahead;
		this.orientation = 0;
	}

	public SnakePiece(Sprite s, SnakePiece ahead) {
		super(s);
		this.ahead = ahead;
	}

	public void tick(int deltaMS) {
		; // TODO animation here instead of per advance?
	}

	public void stepForward() {
		nextFrame();
		if (ahead != null) {
			// follow the one ahead of me
			col = ahead.col;
			row = ahead.row;
			setOrientation(ahead.orientation );
		} else {
			// head
			switch (orientation) {
			case SnakePiece.O_DOWN:
				row++;
				break;
			case SnakePiece.O_UP:
				row--;
				break;
			case SnakePiece.O_RIGHT:
				col++;
				break;
			case SnakePiece.O_LEFT:
				col--;
				break;
			}
		}
	}

	public void setOrientation(int orient) {
		this.orientation = orient;
		switch (orientation) {
		case O_RIGHT:
			setTransform(TRANS_NONE);
			break;
		case O_DOWN:
			setTransform(TRANS_ROT90);
			break;
		case O_LEFT:
			setTransform(TRANS_MIRROR);
			break;
		case O_UP:
			setTransform(TRANS_MIRROR_ROT90);
			break;
		}
	}
}
